﻿using NotaManager;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WpfClient.Models;
using WpfClient.Views;
using MahApps.Metro;
using MahApps.Metro.Controls;
 

namespace WpfClient
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : MetroWindow
    {
        private INotaManager _manager;

        public MainWindow(MainViewModel model, INotaManager manager)
        {
            InitializeComponent();

            this.DataContext = model;
            model.Window = this;
            _manager = manager;
        }

        public MainWindow()
        {
            this.Activated += MainWindow_Activated;
        }

        protected override void OnClosed(EventArgs e)
        {
            
            base.OnClosed(e);
            Application.Current.Shutdown();
        }

    

        async private void Button_Click(object sender, RoutedEventArgs e)
        {
            var file = (FileViewModel)FilesBox.SelectedItem;
            var line = (LineViewModel)Line.Content;
            var temp = new NewTranslation(){TargetLineNotaID = line.Line.NotaID, TargetFileNotaID = file.File.NotaID, Value = InputedText.Text};

            var login = SettingsManager.Settings.Username;
            var pass = SettingsManager.Settings.Password;

            var res = await _manager.Login(login, pass);
            switch (res.Status)
            {
               
                case RequestStatus.ConnectionError:
                    {
                        MessageBox.Show("Connection error: Nota is down!");
                        return;
                    }
                case RequestStatus.ServerRefuse:
                    {
                        MessageBox.Show("Nota doesn't like you: possibly login or pass incorrect");
                        return;
                    }
           
            }

            PostButton.IsEnabled = false;
            PostButton.Content = "Your operetion in progress...";

            await _manager.Post(temp);

            PostButton.IsEnabled = true;
            PostButton.Content = "Post to Nota";
            InputStatusText.Text = "Succesfully posted!";
            InputStatusText.Foreground = Brushes.Red;
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            var text = (string)((Button)sender).Tag;
            InputedText.Text = text;
        }

    
        private void LinesList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            InputedText.Text = "";
            InputStatusText.Text = "Ваш новый перевод (хД)";
            InputStatusText.Foreground = Brushes.Black;
        }


       

        public event Action WindowActivated;

        void MainWindow_Activated(object sender, EventArgs e)
        {
            if (WindowActivated != null)
            {
                WindowActivated();
            }
        }

      

        public void SelectLine(LineViewModel model, FileViewModel fileModel)
        {
            this.FilesBox.SelectedItem = fileModel;
            this.LinesList.SelectedItem = model;
        }

        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {

        }

       
    }
}
